<?php
/**
 * File: storage-driver.php
 * Author: David and Mihaela
 * Created: 11/15/12
 * Last updated: 11/16/2012
 * Purpose: Has test cases for the storage.php file, created for the 1st 
 * iteration of the Internship Listing project. 
 */

require_once("storage.php");
require_once("util.php");

drive( );

/**
 *	Runs the driver
 */
function drive() {
	testInsertInternship( );
	testSelectAllInternships( );
	testDeleteInternship( );
	testSelectSortedInternships("internshipID");
	testSelectSortedInternships("sponsorName");
  testSelectSortedInternships("internshipTitle");
  testSelectSortedInternships("sponsorCity");
  testSelectSortedInternships("internshipCategory");
}

/**
 *  Tests the insertInternship statement using default data. Tests if add was 
 *  completed by trying to add an entry using preset data, and then tries to 
 *  select the new entry from the database. If the new entry is not found 
 * ($results == 0), the add did not occur.
 */
function testInsertInternship( )
{
	echo("<h3>Testing insert into database...</h3>");
	// Sample data
	$testName = "John Doe";
	$testCity = "Manchester";
	$testState = "NH";
	$testDate  = "2012/01/10";
	$testTitle = "Help Desk";
	$testCategory = "Summer Internship";
	$testIsPaid = "yes";
	
	// Perform the insert
	insertInternship($testName, $testCity, $testState, $testDate, $testTitle, 
						$testCategory, $testIsPaid);
	// New rows post insert
	$query = "select * from internship WHERE sponsorName='$testName' AND 
					sponsorCity='$testCity' AND sponsorState='$testState' AND 
					postingDate='$testDate' AND internshipTitle='$testTitle' AND 
					internshipCategory='$testCategory' AND isPaid='$testIsPaid'";
	$results = getRowCount($query);
	// Check to see if a new row has been added
	if($results < 1)
		echo("<font color='red'> - Insert failed: Cannot find new entry</font>");
	else
		echo("<font color='green'>- Insert successful!</font>");
	echo("<hr />");
}

/**
 *	Counts the current number of rows in the table
 */
function getRowCount($query) {
	$conn = connect();
	$results = mysqli_query($conn, $query);
	mysqli_close($conn); //close connection
	return mysqli_num_rows($results);
}

/**
 *	Tests the testSelectAllInternships() function to see if an array is returned.
 *  Calls the selectAllInternships() function and tests to see if either false 
 *  or null has been returned which will signal the error.
 */
function testSelectAllInternships() {
	echo("<h3>Testing select from the database...</h3>");
	// Try the select
	$testArray = selectAllInternships( );
	if($testArray !== null) {
		echo("<pre><font color='green'>");
		var_dump($testArray); 
		echo("</font></pre>");
	} else {
		echo("Error retrieving data from database!<br />");
	}
	echo("<hr />");
}

/* commented out the var dump - brian 12/12/12 */


/**
 *	Tests deleteInternship(), which takes an internshipID as a parameter. 
 *  Performs the delete and tests whether or not it can retrieve the row with 
 *  that ID. If the number of rows returned is greater than 0, the row with the 
 *  specified id is still in the database. If not, the deletion was successful.
 *  Also checks to see if that record existsed before deletion, if not, tell
 *  the user that this record did not exist. 
 */
function testDeleteInternship() {
	echo("<h3>Testing delete from the database ...</h3>");
	$deleteID = "0";
	$preResults=getRowCount("SELECT * from internship WHERE internshipID=$deleteID");
	deleteInternship($deleteID);
	$postResults=getRowCount("SELECT * from internship WHERE internshipID=$deleteID");
	if($postResults < 1 && $preResults > 0) {
		echo("<font color='green'>- Delete successful!</font>");
	} elseif($postResults > 0 && $preResults > 0) {
		echo("<font color='red'> - Delete failed</font>");
	} else {
		echo("<font color='gold'> - Results inconclusive: Internship $deleteID could not be found</font>");
	}
	echo("<hr />");
}

/**
 * Tests selectSortedInternships( ). 
 * Calls selectSortedInternships using $criterion parameter. 
 * Loops through the array to check if previous item is lower than the current
 * item. If not, the array is not sorted in ascending order and an error is 
 * returned. 
 */
function testSelectSortedInternships($criterion) {
	echo("<h3>Testing Sorted Internships...</h3>");
	$results = selectSortedInternships($criterion);
	$last = "";
	$msg = "<font color='green'>- Internships were ordered by $criterion</font>";
	$resultString = "";
	foreach($results as $result) {
		if(!($last <= strtolower($result[$criterion]))) {
			$msg = sprintf("<font color='red'>- Internships were not ordered by %s
                      </font><br />----> ID: %d is out of order<br />", 
                      $criterion, $result['internshipID']);
			$resultString .= "<font color='red'>{$result[$criterion]},&nbsp;</font>";
			break;
		} else {
			$resultString .= "<font color='green'>{$result[$criterion]},&nbsp;</font>";
		}
		$last = strtolower($result[$criterion]);
	}
	echo("$msg<br /><br /> - $resultString<hr />");
}
?>